<!doctype html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Zookeeper 数据同步与一致性机制</title>
    <link rel="stylesheet" href="https://cdn.staticfile.org/font-awesome/6.4.0/css/all.min.css">
    <link rel="stylesheet" href="https://cdn.staticfile.org/tailwindcss/2.2.19/tailwind.min.css">
    <link href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@400;500;600;700&family=Noto+Sans+SC:wght@300;400;500;700&display=swap" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/mermaid@latest/dist/mermaid.min.js"></script>
    <style>
        body {
            font-family: 'Noto Sans SC', Tahoma, Arial, Roboto, "Droid Sans", "Helvetica Neue", "Droid Sans Fallback", "Heiti SC", "Hiragino Sans GB", Simsun, sans-serif;
            background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
            min-height: 100vh;
        }
        
        .hero-gradient {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
        }
        
        .card-hover {
            transition: all 0.3s ease;
            border: 1px solid transparent;
        }
        
        .card-hover:hover {
            transform: translateY(-5px);
            box-shadow: 0 20px 40px rgba(0,0,0,0.1);
            border-color: #667eea;
        }
        
        .text-gradient {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            -webkit-background-clip: text;
            -webkit-text-fill-color: transparent;
            background-clip: text;
        }
        
        .section-title {
            position: relative;
            padding-left: 20px;
        }
        
        .section-title::before {
            content: '';
            position: absolute;
            left: 0;
            top: 50%;
            transform: translateY(-50%);
            width: 4px;
            height: 24px;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            border-radius: 2px;
        }
        
        .list-item {
            position: relative;
            padding-left: 30px;
        }
        
        .list-item::before {
            content: '';
            position: absolute;
            left: 0;
            top: 12px;
            width: 8px;
            height: 8px;
            background: #667eea;
            border-radius: 50%;
            opacity: 0.6;
        }
        
        .mermaid {
            background: white;
            padding: 20px;
            border-radius: 12px;
            box-shadow: 0 10px 30px rgba(0,0,0,0.1);
        }
        
        .highlight-box {
            background: linear-gradient(135deg, rgba(102, 126, 234, 0.1) 0%, rgba(118, 75, 162, 0.1) 100%);
            border-left: 4px solid #667eea;
            padding: 20px;
            border-radius: 8px;
            margin: 20px 0;
        }
        
        @keyframes fadeInUp {
            from {
                opacity: 0;
                transform: translateY(30px);
            }
            to {
                opacity: 1;
                transform: translateY(0);
            }
        }
        
        .animate-fadeInUp {
            animation: fadeInUp 0.8s ease-out;
        }
    </style>
</head>
<body>
    <!-- Hero Section -->
    <div class="hero-gradient text-white py-20">
        <div class="container mx-auto px-6">
            <div class="max-w-4xl mx-auto text-center animate-fadeInUp">
                <h1 class="text-5xl font-bold mb-6" style="font-family: 'Noto Serif SC', serif;">
                    Zookeeper 数据同步与一致性机制
                </h1>
                <p class="text-xl opacity-90 leading-relaxed">
                    深入解析分布式协调服务的核心特性，探索节点间数据同步的奥秘
                </p>
                <div class="mt-8 flex justify-center space-x-4">
                    <span class="inline-flex items-center px-4 py-2 bg-white bg-opacity-20 rounded-full text-sm">
                        <i class="fas fa-sync-alt mr-2"></i> 数据同步
                    </span>
                    <span class="inline-flex items-center px-4 py-2 bg-white bg-opacity-20 rounded-full text-sm">
                        <i class="fas fa-check-circle mr-2"></i> 强一致性
                    </span>
                    <span class="inline-flex items-center px-4 py-2 bg-white bg-opacity-20 rounded-full text-sm">
                        <i class="fas fa-shield-alt mr-2"></i> 故障恢复
                    </span>
                </div>
            </div>
        </div>
    </div>

    <!-- Main Content -->
    <div class="container mx-auto px-6 py-12">
        <div class="max-w-5xl mx-auto">
            
            <!-- Introduction -->
            <div class="bg-white rounded-2xl shadow-xl p-8 mb-8 card-hover">
                <p class="text-lg leading-relaxed text-gray-700 indent-8">
                    在 Zookeeper 中，节点间的数据同步和一致性是其核心特性之一。Zookeeper 使用了一些机制来确保节点间的数据一致性和正确处理节点数据不一致的情况。
                </p>
            </div>

            <!-- 数据同步机制 -->
            <section class="mb-12">
                <h2 class="text-3xl font-bold mb-8 section-title text-gray-800">
                    节点间的数据同步机制
                </h2>

                <!-- 一致性模型 -->
                <div class="bg-white rounded-2xl shadow-xl p-8 mb-6 card-hover">
                    <h3 class="text-2xl font-semibold mb-4 text-gradient">
                        <i class="fas fa-cube mr-3"></i>Zookeeper 的一致性模型
                    </h3>
                    <p class="text-gray-700 mb-6 leading-relaxed">
                        Zookeeper 实现了一个强一致性模型，即在任何时刻，所有客户端都能看到一致的数据视图。这种一致性模型由 Zookeeper 的协议和机制保证，包括以下几个方面：
                    </p>
                    
                    <div class="grid md:grid-cols-2 gap-6">
                        <div class="highlight-box">
                            <h4 class="font-semibold text-lg mb-3 text-purple-700">
                                <i class="fas fa-crown mr-2"></i>Leader 和 Follower 角色
                            </h4>
                            <p class="text-gray-600">
                                Zookeeper 集群中有一个 Leader 节点和多个 Follower 节点。所有的写操作都必须经过 Leader 节点，并由 Leader 节点同步到所有 Follower 节点。只有在大多数节点（包括 Leader）确认写操作后，操作才会被认为成功。
                            </p>
                        </div>
                        
                        <div class="highlight-box">
                            <h4 class="font-semibold text-lg mb-3 text-purple-700">
                                <i class="fas fa-broadcast-tower mr-2"></i>原子广播协议
                            </h4>
                            <p class="text-gray-600">
                                Zookeeper 使用原子广播协议来确保所有节点的数据同步。Leader 节点负责将写操作的变更广播到所有 Follower 节点，并确保所有 Follower 节点都成功接收到这些变更。
                            </p>
                        </div>
                    </div>
                </div>

                <!-- 事务日志 -->
                <div class="bg-white rounded-2xl shadow-xl p-8 mb-6 card-hover">
                    <h3 class="text-2xl font-semibold mb-4 text-gradient">
                        <i class="fas fa-file-alt mr-3"></i>Zookeeper 的事务日志
                    </h3>
                    <div class="bg-gray-50 rounded-lg p-6">
                        <p class="text-gray-700 leading-relaxed">
                            <strong class="text-purple-700">事务日志：</strong>每个 Zookeeper 节点都维护一个事务日志，记录所有的写操作。事务日志用于在节点重新启动或恢复时重建数据状态。Zookeeper 在收到写请求后，会将请求记录到事务日志中，然后将变更广播到其他节点。
                        </p>
                    </div>
                </div>

                <!-- 数据同步过程 -->
                <div class="bg-white rounded-2xl shadow-xl p-8 mb-6 card-hover">
                    <h3 class="text-2xl font-semibold mb-6 text-gradient">
                        <i class="fas fa-sync mr-3"></i>数据同步过程
                    </h3>
                    
                    <div class="space-y-6">
                        <div class="flex items-start">
                            <div class="flex-shrink-0 w-12 h-12 bg-purple-100 rounded-full flex items-center justify-center mr-4">
                                <span class="text-purple-700 font-bold">1</span>
                            </div>
                            <div>
                                <h4 class="font-semibold text-lg mb-2">请求处理</h4>
                                <p class="text-gray-600">客户端发起写请求时，Zookeeper 集群中的 Leader 节点处理请求并将变更应用到本地数据存储。Leader 节点将变更记录到事务日志，并将变更发送到所有 Follower 节点。</p>
                            </div>
                        </div>
                        
                        <div class="flex items-start">
                            <div class="flex-shrink-0 w-12 h-12 bg-purple-100 rounded-full flex items-center justify-center mr-4">
                                <span class="text-purple-700 font-bold">2</span>
                            </div>
                            <div>
                                <h4 class="font-semibold text-lg mb-2">变更传播</h4>
                                <p class="text-gray-600">Follower 节点接收到变更后，将变更应用到本地数据存储，并记录到事务日志中。所有节点都必须在变更被应用后向 Leader 节点确认收到变更。</p>
                            </div>
                        </div>
                        
                        <div class="flex items-start">
                            <div class="flex-shrink-0 w-12 h-12 bg-purple-100 rounded-full flex items-center justify-center mr-4">
                                <span class="text-purple-700 font-bold">3</span>
                            </div>
                            <div>
                                <h4 class="font-semibold text-lg mb-2">变更确认</h4>
                                <p class="text-gray-600">一旦 Leader 节点收到了大多数节点的确认，它将返回操作成功的响应给客户端。此时，所有节点的数据已经一致，确保了强一致性。</p>
                            </div>
                        </div>
                    </div>
                </div>

                <!-- 数据同步流程图 -->
                <div class="bg-white rounded-2xl shadow-xl p-8 mb-6">
                    <h3 class="text-xl font-semibold mb-4 text-center text-gray-700">数据同步流程图</h3>
                    <div class="mermaid">
                        graph LR
                            A[客户端] -->|写请求| B[Leader节点]
                            B -->|记录事务日志| C[Leader本地存储]
                            B -->|广播变更| D[Follower节点1]
                            B -->|广播变更| E[Follower节点2]
                            B -->|广播变更| F[Follower节点3]
                            D -->|确认| B
                            E -->|确认| B
                            F -->|确认| B
                            B -->|成功响应| A
                            
                            style A fill:#f9f,stroke:#333,stroke-width:2px
                            style B fill:#bbf,stroke:#333,stroke-width:2px
                            style C fill:#dfd,stroke:#333,stroke-width:2px
                            style D fill:#ffd,stroke:#333,stroke-width:2px
                            style E fill:#ffd,stroke:#333,stroke-width:2px
                            style F fill:#ffd,stroke:#333,stroke-width:2px
                    </div>
                </div>
            </section>

            <!-- 节点数据不一致时的处理 -->
            <section class="mb-12">
                <h2 class="text-3xl font-bold mb-8 section-title text-gray-800">
                    节点数据不一致时的处理
                </h2>

                <!-- 故障恢复 -->
                <div class="bg-white rounded-2xl shadow-xl p-8 mb-6 card-hover">
                    <h3 class="text-2xl font-semibold mb-6 text-gradient">
                        <i class="fas fa-tools mr-3"></i>故障恢复
                    </h3>
                    
                    <div class="grid md:grid-cols-2 gap-6">
                        <div class="bg-blue-50 rounded-lg p-6">
                            <h4 class="font-semibold text-lg mb-3 text-blue-700">
                                <i class="fas fa-redo mr-2"></i>节点重启
                            </h4>
                            <p class="text-gray-700">
                                当节点发生故障或重启时，它会从 Leader 节点获取最新的事务日志并恢复数据状态。Leader 节点负